import 'package:get/get.dart';
import 'package:vape_xe/services/services.dart';
import 'package:vape_xe/utils/utils.dart';

class UpgradeController extends GetxController {
  /// 响应式成员变量
  final upgradeData = <String, dynamic>{}.obs;
  final showProgress = true.obs;

  /// 事件
  Future<void> upgrade() async {
    if (upgradeData['userNum'] < upgradeData['targetNum']) return;
    Loading.show();
    await Request()
        .post(
      '/userVape/upgrade',
      {
        'targetId': Get.arguments['targetId'],
      },
      false,
    )
        .then((_) async {
      EventService.to.sendMessage(EventService.EventRefresh);
      await Request().post('/userVape/upgradeInfo', {
        'targetId': Get.arguments['targetId'],
      }).then((result) {
        if (result['result']['isMax'] == 1) {
          //已经是最高等级
          toast('Success, It\'s top level now!');
          Get.back();
        } else {
          toast('Success');
          upgradeData.value = result['result'];
        }
      });
    });
  }

  /// 生命周期
  @override
  void onReady() async {
    super.onReady();
    await Request().post('/userVape/upgradeInfo', {
      'targetId': Get.arguments['targetId'],
    }).then((result) {
      showProgress.value = false;
      upgradeData.value = result['result'];
    });
  }
}
